﻿<template>
    <div class="SHZY-common-layout">
        <div class="SHZY-common-layout-center">
            <el-row class="SHZY-common-search-box" :gutter="16">
                <el-form @submit.native.prevent="">
                    <el-col :span="6">
                        <el-form-item label="工单编号">
                            <el-input v-model="query.no" placeholder="工单编号" clearable @keyup.enter.native="search" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="6">
                        <el-form-item label="客户编码">
                            <el-input v-model="query.customerNo" placeholder="客户编码" clearable
                                @keyup.enter.native="search" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="6">
                        <el-form-item label="客户名称">
                            <el-input v-model="query.customerName" placeholder="客户名称" clearable
                                @keyup.enter.native="search" />
                        </el-form-item>
                    </el-col>
                    <template v-if="showAll">
                        <el-col :span="6">
                            <el-form-item label="反馈阶段">
                                <el-input v-model="query.feedbackStage" placeholder="反馈阶段" clearable
                                    @keyup.enter.native="search" />
                            </el-form-item>
                        </el-col>
                        <el-col :span="6">
                            <el-form-item label="状态">
                                    <el-select v-model="query.status" placeholder="状态" clearable>
                                    <el-option v-for="(item, index) in statusList" :key="index"
                                        :label="item.fullName" :value="item.id" />
                                </el-select>

                            </el-form-item>
                        </el-col>
                        <el-col :span="6">
                            <el-form-item label="反馈日期">
                                <el-date-picker v-model="query.feedbackDate" type="daterange" value-format="timestamp"
                                    format="yyyy-MM-dd" start-placeholder="开始日期" end-placeholder="结束日期">
                                </el-date-picker>
                            </el-form-item>
                        </el-col>
                        <el-col :span="6">
                            <el-form-item label="反馈人">
                                <el-input v-model="query.feedbackUserName" placeholder="反馈人" clearable
                                    @keyup.enter.native="search" />
                            </el-form-item>
                        </el-col>
                        <el-col :span="6">
                            <el-form-item label="反馈方式">
                                <el-input v-model="query.feedbackWay" placeholder="反馈方式" clearable
                                    @keyup.enter.native="search" />
                            </el-form-item>
                        </el-col>
                        <el-col :span="6">
                            <el-form-item label="反馈单号">
                                <el-input v-model="query.feedbackNo" placeholder="反馈单号" clearable
                                    @keyup.enter.native="search" />
                            </el-form-item>
                        </el-col>
                        <el-col :span="6">
                            <el-form-item label="抱怨类型">
                                <el-select v-model="query.complaintType" placeholder="抱怨类型" clearable>
                                    <el-option v-for="(item, index) in complaintTypeOptions" :key="index"
                                        :label="item.fullName" :value="item.id" />
                                </el-select>
                            </el-form-item>
                        </el-col>
                        <el-col :span="6">
                            <el-form-item label="产品名称">
                                <el-input v-model="query.productName" placeholder="产品名称" clearable
                                    @keyup.enter.native="search" />
                            </el-form-item>
                        </el-col>
                      
                    </template>
                    <el-col :span="6">
                        <el-form-item>
                            <el-button type="primary" icon="el-icon-search" @click="search()">查询</el-button>
                            <el-button icon="el-icon-refresh-right" @click="reset()">重置</el-button>
                            <el-button type="text" icon="el-icon-arrow-down" @click="showAll = true"
                                v-if="!showAll">展开</el-button>
                            <el-button type="text" icon="el-icon-arrow-up" @click="showAll = false" v-else>收起</el-button>
                        </el-form-item>
                    </el-col>
                </el-form>
            </el-row>
            <div class="SHZY-common-layout-main SHZY-flex-main">
                <div class="SHZY-common-head">
                    <div>
                        <el-button type="primary" icon="el-icon-plus" @click="addOrUpdateHandle()">新增</el-button>
                        <el-button type="text" icon="el-icon-download" @click="exportData()">导出</el-button>
                        <el-button type="text" icon="el-icon-delete" @click="handleBatchRemoveDel()">批量删除</el-button>
                        <el-button type="text" icon="el-icon-close" @click="handleBatchClose()">关闭工单</el-button>
                    </div>
                    <div class="SHZY-common-head-right">
                        <el-tooltip effect="dark" content="刷新" placement="top">
                            <el-link icon="icon-ym icon-ym-Refresh SHZY-common-head-icon" :underline="false"
                                @click="reset()" />
                        </el-tooltip>
                    </div>
                </div>
                <SHZY-table v-loading="listLoading" :data="list" has-c @selection-change='handleSelectionChange'
                     custom-column>

                    <el-table-column label="当前进度" prop="" align="left" width="100" show-overflow-tooltip>
                        <template slot-scope="scope">
                            <el-tag v-if="scope.row.status === 0" type="info">20%</el-tag>
                            <el-tag v-if="scope.row.status === 1" type="info">40%</el-tag>
                            <el-tag v-if="scope.row.status === 2" type="success">60%</el-tag>
                            <el-tag v-if="scope.row.status === 3" type="success">80%</el-tag>
                            <el-tag v-if="scope.row.status === 4" type="success">100%</el-tag>
                        </template>
                    </el-table-column>
                    <el-table-column label="工单编号" prop="no" align="left" width="100" show-overflow-tooltip>
                    </el-table-column>
                    <el-table-column label="申请日期" prop="creatorTime" align="left" width="150" show-overflow-tooltip>
                        <template slot-scope="scope">{{ scope.row.creatorTime | toDate() }}</template>
                    </el-table-column>
                    <el-table-column label="工单状态" prop="status" align="left" width="100" show-overflow-tooltip>

                        <template slot-scope="scope">
                            <el-tag v-if="scope.row.status === 0" type="info">待处理</el-tag>
                            <el-tag v-if="scope.row.status === 1">处理中</el-tag>
                            <el-tag v-if="scope.row.status === 2" type="warning">已反馈</el-tag>
                            <el-tag v-if="scope.row.status === 3" type="success">已完成</el-tag>
                            <el-tag v-if="scope.row.status === 4" type="danger">已关闭</el-tag>
                        </template>

                    </el-table-column>
                    <el-table-column label="客户编码" prop="customerNo" align="left" width="100" show-overflow-tooltip>
                    </el-table-column>
                    <el-table-column label="客户名称" prop="customerName" align="left" width="100" show-overflow-tooltip>
                    </el-table-column>
                    <el-table-column label="产品名称" prop="productName" align="left" width="100" show-overflow-tooltip>
                    </el-table-column>
                    <el-table-column label="问题描述" prop="problemDescription" align="left" width="100"
                        show-overflow-tooltip>
                    </el-table-column>
                    <el-table-column label="反馈阶段" prop="feedbackStage" align="left" width="100" show-overflow-tooltip>
                    </el-table-column>
                    <el-table-column label="反馈人" prop="feedbackUserName" align="left" width="100" show-overflow-tooltip>
                    </el-table-column>
                    <el-table-column label="反馈方式" prop="feedbackWay" align="left" width="100" show-overflow-tooltip>
                    </el-table-column>
                    <el-table-column label="反馈单号" prop="feedbackNo" align="left" width="100" show-overflow-tooltip>
                    </el-table-column>
                    <el-table-column label="反馈日期" prop="feedbackDate" align="left" width="150">
                        <template slot-scope="scope">{{ scope.row.feedbackDate | toDate() }}</template>
                    </el-table-column>
                    <el-table-column label="抱怨类型" prop="complaintType" align="left" width="100" show-overflow-tooltip>
                    </el-table-column>
                    <el-table-column label="缺陷类别" prop="defectCategory" align="left" width="100" show-overflow-tooltip>
                    </el-table-column>
                    <el-table-column label="创建人" prop="creatorUserId" align="left" width="100" show-overflow-tooltip>
                    </el-table-column>
                    <el-table-column label="关闭日期" prop="cancelDate" align="left" width="150">
                        <template slot-scope="scope">{{ scope.row.cancelDate | toDate() }}</template>
                    </el-table-column>
                    <el-table-column label="操作" fixed="right" width="120">
                        <template slot-scope="scope">
                            <el-button type="text" @click="print(scope.row.id)">打印</el-button>
                            <el-button type="text" @click="addOrUpdateHandle(scope.row.id)"
                                v-if="scope.row.status != 3 && scope.row.status != 4">编辑</el-button>
                                <el-button type="text" @click="addOrUpdateHandle(scope.row.id)"
                                v-else>查看</el-button>
                            <el-button type="text" @click="handleDel(scope.row.id)" class='SHZY-table-delBtn'
                                v-if="!scope.row.status || scope.row.status == 0">删除</el-button>
                        </template>
                    </el-table-column>
                </SHZY-table>
                <pagination :total="total" :page.sync="listQuery.currentPage" :limit.sync="listQuery.pageSize"
                    @pagination="initData" />
            </div>
        </div>
        <SHZY-Form v-if="formVisible" ref="SHZYForm" @refresh="refresh" />
        <ExportBox v-if="exportBoxVisible" ref="ExportBox" @download="download" />
        <selectDialog v-if="selectVisible" ref="selectDialog" />

    </div>
</template>
<script>
import customerAPI from '@/api/q1/business/customer';
import SHZYForm from './Form'
import selectDialog from './selectDialog'
import ExportBox from './ExportBox'


export default {
    components: { SHZYForm, ExportBox, selectDialog },
    data() {
        return {
            detailVisible: false,
            showAll: false,
            query: {
                no: undefined,
                customerNo: undefined,
                customerName: undefined,
                feedbackStage: undefined,
                feedbackDate: undefined,
                feedbackUserName: undefined,
                feedbackWay: undefined,
                feedbackNo: undefined,
                complaintType: undefined,
                productName: undefined,
                status:undefined
            },
            list: [],
            listLoading: true,
            multipleSelection: [],
            total: 0,
            listQuery: {
                currentPage: 1,
                pageSize: 20,
                sort: "desc",
                sidx: "",
            },
            formVisible: false,
            selectVisible: false,
            exportBoxVisible: false,
            columnList: [
                { prop: 'status', label: '状态' },
                { prop: 'no', label: '工单编号' },
                { prop: 'creatorTime', label: '申请日期' },
                { prop: 'customerNo', label: '客户编码' },
                { prop: 'customerName', label: '客户名称' },
                { prop: 'feedbackStage', label: '反馈阶段' },
                { prop: 'feedbackUserName', label: '反馈人' },
                { prop: 'feedbackDate', label: '反馈日期' },
                { prop: 'feedbackWay', label: '反馈方式' },
                { prop: 'feedbackNo', label: '反馈单号' },
                { prop: 'complaintType', label: '抱怨类型' },
                { prop: 'productName', label: '产品名称' },
                { prop: 'defectCategory', label: '缺陷类别' },
                { prop: 'problemDescription', label: '问题描述' },
                { prop: 'cancelDate', label: '关闭日期' },
                { prop: 'creatorUserId', label: '创建人' },
            ],
            complaintTypeOptions: [{ 'fullName': "选项一", 'id': "1" }, { 'fullName': "选项二", 'id': "2" }],
            defectCategoryOptions: [{ 'fullName': "选项一", 'id': "1" }, { 'fullName': "选项二", 'id': "2" }],
            checkProblemOptions: [{ 'fullName': "选项一", 'id': "1" }, { 'fullName': "选项二", 'id': "2" }],
            checkDuplicateOptions: [{ 'fullName': "选项一", 'id': "1" }, { 'fullName': "选项二", 'id': "2" }],
            checkMajorOptions: [{ 'fullName': "选项一", 'id': "1" }, { 'fullName': "选项二", 'id': "2" }],
            isStauts: [],
            statusList: [{ 'fullName': "待处理", 'id': "0" }, { 'fullName': "处理中", 'id': "1" },{ 'fullName': "已反馈", 'id': "2" },{ 'fullName': "已完成", 'id': "3" },{ 'fullName': "已关闭", 'id': "4" }],
        }
    },
    computed: {
        menuId() {
            return this.$route.meta.modelId || ''
        }
    },
    created() {
        this.initData()
    },
    methods: {

        initData() {
            this.listLoading = true;
            let _query = {
                ...this.listQuery,
                ...this.query,
                menuId: this.menuId,
            };
            let query = {}
            for (let key in _query) {
                if (Array.isArray(_query[key])) {
                    query[key] = _query[key].join()
                } else {
                    query[key] = _query[key]
                }
            }

            customerAPI.qmsbusicustomercomplaint(query).then(res => {
                this.list = res.data.list
                this.total = res.data.pagination.total
                this.listLoading = false
            });



        },
        handleDel(id) {
            this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
                type: 'warning'
            }).then(() => {
                customerAPI.deletetQmsbusicustomercomplaint(id).then(res => {
                    this.$message({
                        type: 'success',
                        message: res.msg,
                        onClose: () => {
                            this.initData()
                        }
                    });

                })
            }).catch(() => {
            });
        },
     

        handleSelectionChange(val) {
            const res = val.map(item => item.id)
            this.multipleSelection = res
            this.isStauts = val.map(item => item.status)


        },
        handleBatchRemoveDel() {
            if (!this.multipleSelection.length) {
                this.$message({
                    type: 'error',
                    message: '请选择一条数据',
                    duration: 1500,
                })
                return
            }

            if (this.isStauts.indexOf(1) != -1 || this.isStauts.indexOf(2) != -1 || this.isStauts.indexOf(3) != -1 || this.isStauts.indexOf(4) != -1) {
                this.$message({
                    type: 'error',
                    message: '选中的数据不能删除',
                    duration: 1500,
                })
                return

            }



            const ids = this.multipleSelection
            this.$confirm('您确定要删除这些数据吗, 是否继续？', '提示', {
                type: 'warning'
            }).then(() => {

                customerAPI.batchRemove(ids).then(res => {
                    this.$message({
                        type: 'success',
                        message: res.msg,
                        onClose: () => {
                            this.initData()
                        }
                    });
                })

            }).catch(() => { })
        },


        handleBatchClose() {
            if (!this.multipleSelection.length) {
                this.$message({
                    type: 'error',
                    message: '请选择一条数据',
                    duration: 1500,
                })
                return
            }
            console.log("🚀 ~ this.isStauts:", this.isStauts)
            if (this.isStauts.indexOf(null) != -1 || this.isStauts.indexOf(0) != -1 || this.isStauts.indexOf(1) != -1 || this.isStauts.indexOf(2) != -1) {
                
                this.$message({
                    type: 'error',
                    message: '选中的数据不能关闭',
                    duration: 1500,
                })
                return
            }

            const ids = this.multipleSelection
            this.$confirm('您确定要关闭这些数据吗, 是否继续？', '提示', {
                type: 'warning'
            }).then(() => {

                customerAPI.CloseWorkOrder(ids).then(res => {
                    this.$message({
                        type: 'success',
                        message: res.msg,
                        onClose: () => {
                            this.initData()
                        }
                    });
                })

            }).catch(() => { })
        },


        addOrUpdateHandle(id) {
            this.formVisible = true
            this.$nextTick(() => {
                this.$refs.SHZYForm.init(id)
            })
        },
        exportData() {
            this.exportBoxVisible = true
            this.$nextTick(() => {
                this.$refs.ExportBox.init(this.columnList)
            })
        },
        download(data) {
            let query = { ...data, ...this.listQuery, ...this.query, menuId: this.menuId }

            customerAPI.Export(query).then(res => {

                if (!res.data.url) return
                this.shzy.downloadFile(res.data.url)
                this.$refs.ExportBox.visible = false
                this.exportBoxVisible = false

            })

        },
        search() {
            this.listQuery = {
                currentPage: 1,
                pageSize: 20,
                sort: "desc",
                sidx: "no",
            }
            this.initData()
        },
        refresh(isrRefresh) {
            this.formVisible = false
            if (isrRefresh) this.reset()
        },
        reset() {
            for (let key in this.query) {
                this.query[key] = undefined
            }
            this.listQuery = {
                currentPage: 1,
                pageSize: 20,
                sort: "desc",
                sidx: "no",
            }
            this.initData()
        },

        print(id) {
            this.selectVisible = true
            this.$nextTick(() => {
                this.$refs.selectDialog.init(id)
            })

        }

    }
}
</script>